//
//  recordSummary.m
//  babykicks
//
//  Created by 彤辉 沈 on 12-2-14.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "recordSummary.h"

@implementation recordSummary

@synthesize startTime,recordTime;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self openDB];
    startTime = [self selectStarttime];
    recordTime = [self selectRecordtime];
    start = [[NSString alloc]init];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [startTime count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    // Configure the cell...
    
    start = [startTime objectAtIndex:indexPath.row];
    int count = 0;
    for (int i=0; i<[[recordTime objectAtIndex:0]count]; i++) {
        if ([start isEqualToString:[[recordTime objectAtIndex:0]objectAtIndex:i]]) {
            count++;
        }
    }
    cell.textLabel.text = start;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"共记录了%i次",count];
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

-(BOOL)openDB{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:@"db.sql"];
    NSFileManager* manager = [NSFileManager defaultManager];
    BOOL find = [manager fileExistsAtPath:path];
    
    //若找到数据库文件
    if (find) {
        NSLog(@"db file exists");
        //若无法打开文件
        if (sqlite3_open([path UTF8String], &database)!=SQLITE_OK) {
            sqlite3_close(database);
            NSLog(@"cannot open file");
            return NO;
        }
        return YES;
    }
    
    //若未找到文件，则创建之并打开
    if (sqlite3_open([path UTF8String], &database)==SQLITE_OK) {
        //[self createTable:database];//在后面实现函数createChannelsTable
        return YES;
    }else {
        sqlite3_close(database);
        NSLog(@"Error: open database file.");
        return NO;
    }
    return NO;
}

-(NSArray*)selectStarttime{
    NSMutableArray* result = [[NSMutableArray alloc]init];
    char* sql = "select starttime from starttime";
    sqlite3_stmt* statment;
    if (sqlite3_prepare_v2(database, sql, -1, &statment, nil)==SQLITE_OK) {
        while (sqlite3_step(statment)==SQLITE_ROW) {
            char* time = (char*)sqlite3_column_text(statment, 0);
            [result addObject:[NSString stringWithUTF8String:time]];
        }
    }
    return result;
}

-(NSArray*)selectRecordtime{
    NSMutableArray* result = [[NSMutableArray alloc]init];
    NSMutableArray* startArray = [[NSMutableArray alloc]init];
    NSMutableArray* recordArray = [[NSMutableArray alloc]init];
    char* sql = "select * from recordtime";
    sqlite3_stmt* statment;
    if (sqlite3_prepare_v2(database, sql, -1, &statment, nil)==SQLITE_OK) {
        while (sqlite3_step(statment)==SQLITE_ROW) {
            char* starttime = (char*)sqlite3_column_text(statment, 0);
            char* recordtime = (char*)sqlite3_column_text(statment, 1);
            [startArray addObject:[NSString stringWithUTF8String:starttime]];
            [recordArray addObject:[NSString stringWithUTF8String:recordtime]];
        }
    }
    [result addObject:startArray];
    [result addObject:recordArray];
    return result;
}


@end
